const defaultState = {
  articles: [],
  articlesCount: 0,
  loading: false,
  article: {
    title: '',
    description: '',
    body: '',
    tagList: [],
    favoritesCount: 0,
    favorited: false,
    author: {
      username: '',
      bio: '',
      image: '',
      following: false
    }
  }
}

const reducer = (state = defaultState, action) => {
  switch (action.type) {
    case 'setArticles':
      return {
        ...state,
        articles: action.articles,
      }
    case 'setArticlesCount':
      return {
        ...state,
        articlesCount: action.articlesCount
      }
    case 'setArticlesLoading':
      return {
        ...state,
        loading: action.loading
      }
    case 'setArticle':
      return {
        ...state,
        article: { ...action.article }
      }
    case 'updateArticle':
      return {
        ...state,
        article: {
          ...state.article,
          [action.key]: action.val
        }
      }
    default:
      return state
  }
}

export default reducer